<?php
/*
 *  mipush 相关
 *
 */

class mipush {
    private $app_secret;
    private $restricted_package_name;

    private $apis = array(
        "reg_url"   => 'https://api.xmpush.xiaomi.com/v2/message/regid',
        "alias_url" => 'https://api.xmpush.xiaomi.com/v2/message/alias',
        "topic_url" => 'https://api.xmpush.xiaomi.com/v2/message/topic',
        "stats_url" => 'https://api.xmpush.xiaomi.com/v1/stats/message/counters',
        "multi_messages_regids_url"  => 'https://api.xmpush.xiaomi.com/v2/multi_messages/regids',
        "multi_messages_aliases_url" => 'https://api.xmpush.xiaomi.com/v2/multi_messages/aliases',
    );

    private $sandbox_apis = array(
        "reg_url"   => 'https://sandbox.xmpush.xiaomi.com/v2/message/regid',
        "alias_url" => 'https://sandbox.xmpush.xiaomi.com/v2/message/alias',
        "topic_url" => 'https://sandbox.xmpush.xiaomi.com/v2/message/topic',
        "stats_url" => 'https://api.xmpush.xiaomi.com/v1/stats/message/counters',
        "multi_messages_regids_url"  => 'https://sandbox.xmpush.xiaomi.com/v2/multi_messages/regids',
        "multi_messages_aliases_url" => 'https://sandbox.xmpush.xiaomi.com/v2/multi_messages/aliases',
    );

    public function send($clients, $info, $appType = 0)
    {
        if($appType)
        {
            $this->app_secret = 'Q9W1i20bAqRZf+CTtKjGmQ==';
            $this->restricted_package_name = 'com.hupu.Joggers';
        }
        else
        {
            $this->app_secret = "9SwqsQDwbeWpwirB04+1aQ==";
            $this->restricted_package_name = "com.hupu.joggers";
        }

        $result = FALSE;
        if(!empty($clients)) {
            $client = join(",", $clients);
            $result = $this->sendToAlias($client, $info);
        }
        return $result;
    }

    private function sendToAlias($client, $info) {
        $fields = array(
            'payload' => json_encode($info),
            'time_to_live' => 60*60*1000,
            'restricted_package_name' => $this->restricted_package_name,
            'alias' => $client,
            'title' => !empty($info["aps"]["alert"]["title"]) ? $info["aps"]["alert"]["title"] : $info["aps"]["alert"]["body"],
            'description' => $info["aps"]["alert"]["body"], 
            'notify_foreground' => 0,
            'notify_type' => -1, 
            // 'notify_id' => $notify_id, 
            'pass_through' => 0,
            // 'extra.sound_uri' => $sound_uri,
            'extra.intent_uri' => $info["url"],
            'extra.web_uri' => $info["url"],
            'extra.url' => $info['url'],
        );  

        $headers = array('Authorization: key=' . $this->app_secret, 'Content-Type: application/x-www-form-urlencoded');

        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $this->apis["alias_url"]);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);
        $result = $result ? json_decode($result, TRUE) : array();

        return $result;
    }
}